package com.reform.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.reform.config.JwtTokenUtil;
import com.reform.pojo.expert.Expert;
import com.reform.mapper.ExpertMapper;
import com.reform.pojo.phase.ViewBatch;
import com.reform.pojo.project.PhaseTime;
import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.IExpertService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reform.service.IPhaseTimeService;
import com.reform.service.IViewBatchService;
import com.reform.utils.VerifycUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mortale
 * @since 2021-06-26
 */
@Service
public class ExpertServiceImpl extends ServiceImpl<ExpertMapper, Expert> implements IExpertService {

    @Autowired
    ExpertMapper expertMapper;
    @Autowired
    IViewBatchService viewBatchService;
    @Autowired
    JwtTokenUtil jwtTokenUtil;
    @Autowired
    IPhaseTimeService phaseTimeService;

    /**
     * 验证是否专家
     *
     * @param gh
     * @return
     */
    @Override
    public boolean chargeExpertAuth(String gh) {
        List<Expert> expert;
        expert = expertMapper.selectList(new QueryWrapper<Expert>().eq("gh", gh));
        return expert != null;
    }

    @Override
    public Result getExpertByBatchId(String batchId) {
        return Result.success(ResultCode.SUCCESS, expertMapper.selectList(new QueryWrapper<Expert>().eq("batch_id", batchId)));
    }

    @Override
    public Result creatExpert(String experts) throws SQLException {
        List<Expert> expertList;
        //解析JSON
        try {
            expertList = JSON.parseArray(experts, Expert.class);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(ResultCode.PARAM_JSON_IS_WRONG);
        }
        int res = 0;
        for (Expert expert : expertList) {
            res += expertMapper.insert(expert);
        }
        if (res == expertList.size()) {
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public Result deleteExpert(String experts) throws SQLException {
        List<Expert> expertList;
        //解析JSON
        try {
            expertList = JSON.parseArray(experts, Expert.class);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(ResultCode.PARAM_JSON_IS_WRONG);
        }
        int res = 0;
        for (Expert expert : expertList) {
            res += expertMapper.deleteById(expert);
        }
        if (res == expertList.size()) {
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public Result getExperts() throws SQLException {
        return Result.success(ResultCode.SUCCESS, expertMapper.selectList(null));
    }

    @Override
    public List<String> getBatchIdByGH(String gh, Integer type) throws SQLException {
        return expertMapper.getBatchIdByGH(gh, type);
    }

    @Override
    public Result getBatchInfo(HttpServletRequest request) throws SQLException {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.failure(ResultCode.USER_AUTH_Login_LAPSE);
        }
        String gh = jwtTokenUtil.getGHFromToken(token);
        List<String> batchIdList = getBatchIdByGH(gh, 1);
        List<ViewBatch> viewBatches;
        try {
            viewBatches = viewBatchService.listByIds(batchIdList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.success(ResultCode.SUCCESS, batchIdList);
        }
        return Result.success(ResultCode.SUCCESS, viewBatches);
    }

    @Override
    public Result getPhaseInfo(HttpServletRequest request) throws SQLException {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.failure(ResultCode.USER_AUTH_Login_LAPSE);
        }
        String gh = jwtTokenUtil.getGHFromToken(token);
        List<String> batchIdList = getBatchIdByGH(gh, 2);
        List<PhaseTime> phaseTimes;
        try {
            phaseTimes = phaseTimeService.listByIds(batchIdList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.success(ResultCode.SUCCESS, batchIdList);
        }
        return Result.success(ResultCode.SUCCESS, phaseTimes);
    }

}
